﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

namespace VLXD.AdminControls
{
    public partial class DanhMuc : System.Web.UI.UserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                DataList1.DataSource = DanhMuc_Info.DanhMucs_ChonTatCa();
                DataList1.DataBind();

                //đổi tên hiển thị và danh mục con
                foreach (DataListItem li in DataList1.Items)
                {

                    Label danhmucgoc = (Label)li.FindControl("lblparent");
                    Label active = (Label)li.FindControl("lblactive");
                    
                    if (active.Text.Equals("True"))
                    {
                        active.Text = "Hiển thị";
                    }
                    else
                    {
                        active.Text = "Không hiển thị";
                    }
                    if (danhmucgoc.Text == "0")
                    {
                        danhmucgoc.Text = "Root";
                    }
                    else
                    {
                        danhmucgoc.Text = "Danh Mục Con";
                    }
                }

                MultiView1.ActiveViewIndex = 0;
                
            }
        }


        protected void Button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("admin.aspx?page=danhmuc");
        }
      
        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            //MultiView1.ActiveViewIndex = 1;
            //DropDownList2.DataSource = items.getparent("1");
            //DropDownList2.DataTextField = "names";
            //DropDownList2.DataValueField = "ID";
            //DropDownList2.DataBind();
            //DropDownList2.Items.Add(new ListItem("--Root--", "0"));
        }

        protected void Delete_Load(object sender, EventArgs e)
        {
            ((ImageButton)sender).Attributes["onclick"] = "return confirm('Bạn có muốn xóa mục này không?')";
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
        }
       
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int ID = Convert.ToInt32(HiddenField1.Value);
            int parentID = Convert.ToInt32(DropDownList1.SelectedValue);
            int no = Convert.ToInt32(e_txtvitri.Text.Trim());
            string tieude = e_txtten.Text.Trim();
            int status;
            if (e_CheckBox1.Checked)
            {
                status = 1;
            }
            else
            {
                status = 0;
            }
            //try
            //{
            DanhMuc_Info.DanhMuc_CapNhat(ID, parentID, tieude, no, status);
            Response.Redirect("admin.aspx?page=danhmuc");
            //}
            //catch { }
        }


        private string LoadMenu()
        {
            string htmlMenu = "";
            //DataTable dtbRoot = items.getparent("");
            //if (dtbRoot.Rows.Count > 0)
            //{
            //    foreach (DataRow dr1 in dtbRoot.Rows)
            //    {
            //        htmlMenu += "<a style='text-transform:uppercase'><b>" + dr1["names"] + "</b></a>";
            //        DataTable dtbSub = items.getchild(dr1["ID"].ToString(), "");
            //        if (dtbSub.Rows.Count > 0)
            //        {
            //            htmlMenu += "<div class='vll'><ul>";
            //            foreach (DataRow dr2 in dtbSub.Rows)
            //            {
            //                htmlMenu += "<li><a href='Default.aspx?page=byitem&item=" + dr2["ID"] + "'>" +
            //                            cv(dr2["names"].ToString()) + "</a></li>";
            //            }
            //            htmlMenu += "</ul></div>";
            //        }
            //    }
            //}
            return htmlMenu;
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {

            string name = e.CommandName;
            int ID = Convert.ToInt32(e.CommandArgument.ToString());
            HiddenField1.Value = ID.ToString();
            switch (name)
            {
                case "del":
                   
                    DanhMuc_Info.DanhMuc_Xoa(ID);
                    Response.Redirect("admin.aspx?page=danhmuc");
                    break;
                case "edit":
                    DanhMuc_Info ct = DanhMuc_Info.DanhMuc_Chon(ID);
                  
                    if (ct!=null)
                    {
                        e_txtten.Text = ct.TenDanhMuc;
                        e_txtvitri.Text = ct.SoThuTu.ToString();
                        if (ct.HoatDong)
                            e_CheckBox1.Checked = true;
                        else
                            e_CheckBox1.Checked = false;
                    }
                    
                    if (ct.DanhMucCha != 0)
                    {
                        DropDownList1.DataSource = DanhMuc_Info.DanhMucs_LayDanhMucCha(true);
                        DropDownList1.DataTextField = "TenDanhMuc";
                        DropDownList1.DataValueField = "ID";
                        DropDownList1.DataBind();
                        DropDownList1.SelectedValue = ct.DanhMucCha.ToString();
                    }
                    DropDownList1.Items.Add(new ListItem("--Root--", "0"));
                    MultiView1.ActiveViewIndex = 2;
                    break;
            }
        }

        protected void lbtThemMoi_Click(object sender, EventArgs e)
        {

            MultiView1.ActiveViewIndex = 1;
            DropDownList2.DataSource = DanhMuc_Info.DanhMucs_LayDanhMucCha(true);
            DropDownList2.DataTextField = "TenDanhMuc";
            DropDownList2.DataValueField = "ID";
            DropDownList2.DataBind();
            DropDownList2.Items.Add(new ListItem("--Root--", "0"));
        }

        protected void bntThemMoi_Click(object sender, EventArgs e)
        {
            int parent = Convert.ToInt32(DropDownList2.SelectedValue);
            // Response.Write(parent.ToString());
            int no = Convert.ToInt32(TextBox2.Text.Trim());
            bool status = true;
            if (!ckHoatDong.Checked)
                status = false;
            //try
            //{
            DanhMuc_Info.DanhMuc_Them(parent, txtTenDanhMuc.Text.Trim(), no, status);
            Response.Redirect("admin.aspx?page=danhmuc");
            //}
            //catch { }
        }

        protected void Button4_Click1(object sender, EventArgs e)
        {
            Response.Redirect("admin.aspx?page=danhmuc");
        }
  
    }
}